package com.ykt.mall.ctrl;

import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.ykt.mall.content.Content;
import com.ykt.mall.interceptor.LoginJumpInterceptor;
import com.ykt.model.Customer;
import com.ykt.model.Shop;

@ControllerBind(controllerKey = "/home",viewPath="/common")
public class HomeCtrl  extends BaseWebController<Customer> {
    /**
     * 页面底部登录退出div
     */
	public void loginFoot(){
		
	}
	public void login(){
	}
	public void regist(){}
	@Before(LoginJumpInterceptor.class)
	public void registDo(){
		
		try{
			render("/template/common/regist.html");
			keepPara();
			
			Shop shop = getShop();
			int shopId = 0;
			if(shop != null){
				shopId = shop.getInt("id");
			}
			
			if(Customer.dao.getCustomerByPhone(getPara("phone"), shopId)!=null){
				setAttr("error", "该手机已经注册");
				return ;
			}
			Customer customer=new Customer();
			Date time = new Date();
			customer.set("username", getPara("phone"));
			customer.set("password", getPara("upwd"));
			customer.set("phone", getPara("phone"));
			customer.set("customer_no",time.getTime());
			customer.set("create_time",time);
			customer.set("join_time", time);
			customer.set("shop_id", shopId);
			customer.save();
			setSessionAttr(Content.SESSION_LOGIN_USER, customer);
			redirect("/customer");
		}catch(Exception e){
			e.printStackTrace();
			setAttr("error", "系统错误");
		}
	}
	
	@Before(LoginJumpInterceptor.class)
	public void loginDo(){
		String name = getPara("uname");
		String pwd = getPara("upwd");
		Shop shop = getShop();
		int shopId = 0;
		if(shop != null){
			shopId = shop.getInt("id");
		}
		Customer c = Customer.dao.checkCustomer(name, pwd, shopId);
		if(c==null){
			setAttr("login_error", "用户不存在或密码不正确");
			render("/template/common/login.html");
			return ;
		}else{
			setSessionAttr(Content.SESSION_LOGIN_USER, c);
			redirect("/customer");
		}
	}
	public void logout(){
		removeSessionAttr(Content.SESSION_LOGIN_USER);
		render("/template/common/login.html");
	}
	
}
